package qeug.shsh.opplugin;

import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.entity.DynamicObjectCollection;
import kd.bos.entity.datamodel.events.ChangeData;
import kd.bos.entity.datamodel.events.PropertyChangedArgs;
import kd.bos.form.plugin.AbstractFormPlugin;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.BusinessDataServiceHelper;

//供应商，客户银行账号，统一社会信用代码验重校验逻辑
public class SupplierBankBillPlugin extends AbstractFormPlugin {

    @Override
    public void propertyChanged(PropertyChangedArgs e) {
        super.propertyChanged(e);
        String fieldKey = e.getProperty().getName();
        if ("bankaccount".equals(fieldKey)) {
            //银行账号
            ChangeData[] changeSet = e.getChangeSet();
            ChangeData changeData = changeSet[0];
            int rowIndex = changeData.getRowIndex();//修改行
            String bankAccount = (String) changeData.getNewValue();//银行账号
            if (bankAccount.equals("")){
                return;
            }
            String number = (String) this.getModel().getValue("number");//编码
            QFilter[] qFilters = new QFilter[]{
                    new QFilter("entry_bank.bankaccount", QCP.equals, bankAccount)
            };
            String name = this.getModel().getDataEntityType().getName();//实体编码
            if (name.equals("bd_supplier")) {
                //供应商
                DynamicObject bd_supplier = BusinessDataServiceHelper.loadSingle("bd_supplier", qFilters);//供应商数据
                if (bd_supplier != null) {
                    //查询供应商中是否有存在相同的银行账号,不为空说明在供应商中查询到了相同银行账号的单据，则提示并清空对应银行账号的值
                    this.getModel().setValue("bankaccount", "", rowIndex);
                    this.getView().showErrorNotification("输入的银行账号在供应商单据中存在！！");
                    return;
                }
                DynamicObject bd_customer = BusinessDataServiceHelper.loadSingle("bd_customer", qFilters);//客户数据
                if (bd_customer != null) {
                    String customerNumber = bd_customer.getString("number");//客户编码
                    if (!number.equals(customerNumber)) {
                        //客户编码和供应商编码不相等才校验
                        //查询客户中是否有存在相同的银行账号,不为空说明在客户中查询到了相同银行账号的单据，则提示并清空对应银行账号的值
                        this.getModel().setValue("bankaccount", "", rowIndex);
                        this.getView().showErrorNotification("输入的银行账号在客户单据中存在！！");
                        return;
                    }
                }
            } else {
                //客户
                DynamicObject bd_customer = BusinessDataServiceHelper.loadSingle("bd_customer", qFilters);//客户数据
                if (bd_customer != null) {
                    //查询客户中是否有存在相同的银行账号,不为空说明在客户中查询到了相同银行账号的单据，则提示并清空对应银行账号的值
                    this.getModel().setValue("bankaccount", "", rowIndex);
                    this.getView().showErrorNotification("输入的银行账号在客户单据中存在！！");
                    return;
                }
                DynamicObject bd_supplier = BusinessDataServiceHelper.loadSingle("bd_supplier", qFilters);//供应商数据
                if (bd_supplier != null) {
                    String supplierNumber = bd_supplier.getString("number");//供应商编码
                    if (!number.equals(supplierNumber)) {
                        //供应商编码和客户编码不相等才校验
                        //查询供应商中是否有存在相同的银行账号,不为空说明在供应商中查询到了相同银行账号的单据，则提示并清空对应银行账号的值
                        this.getModel().setValue("bankaccount", "", rowIndex);
                        this.getView().showErrorNotification("输入的银行账号在供应商单据中存在！！");
                        return;
                    }
                }
            }

            //供应商，客户都进入验证
            DynamicObjectCollection entry_bankCollection = this.getModel().getDataEntity(true).getDynamicObjectCollection("entry_bank");//银行信息分录
            for (int i = 0; i < entry_bankCollection.size(); i++) {
                if (i == rowIndex){
                    continue;
                }
                DynamicObject entry_bank = entry_bankCollection.get(i);
                String bankAccount2 = entry_bank.getString("bankaccount");//银行账号
                if (bankAccount.equals(bankAccount2)) {
                    //查询银行信息分录中是否有存在相同的银行账号,相同则提示并清空对应银行账号的值
                    this.getModel().setValue("bankaccount", "", rowIndex);
                    this.getView().showErrorNotification("输入的银行账号在银行信息分录中存在！！");
                    return;
                }
            }
        } else if ("societycreditcode".equals(fieldKey)) {
            //统一社会信用代码
            ChangeData[] changeSet = e.getChangeSet();
            ChangeData changeData = changeSet[0];
            String societyCreditCode = (String) changeData.getNewValue();//统一社会信用代码
            if (societyCreditCode.equals("")){
                return;
            }
            String number = (String) this.getModel().getValue("number");//编码
            QFilter[] qFilters = new QFilter[]{
                    new QFilter("societycreditcode", QCP.equals, societyCreditCode)
            };
            String name = this.getModel().getDataEntityType().getName();//实体编码
            if (name.equals("bd_supplier")) {
                //供应商
                DynamicObject bd_supplier = BusinessDataServiceHelper.loadSingle("bd_supplier", qFilters);//供应商数据
                if (bd_supplier != null) {
                    this.getModel().setValue("societycreditcode", "");
                    this.getView().showErrorNotification("输入的统一社会信用代码在供应商单据中存在！！");
                    return;
                }
                DynamicObject bd_customer = BusinessDataServiceHelper.loadSingle("bd_customer", qFilters);//客户数据
                if (bd_customer != null) {
                    String customerNumber = bd_customer.getString("number");//客户编码
                    if (!number.equals(customerNumber)) {
                        this.getModel().setValue("societycreditcode", "");
                        this.getView().showErrorNotification("输入的统一社会信用代码在客户单据中存在！！");
                        return;
                    }
                }
            } else {
                //客户
                DynamicObject bd_customer = BusinessDataServiceHelper.loadSingle("bd_customer", qFilters);//客户数据
                if (bd_customer != null) {
                    this.getModel().setValue("societycreditcode", "");
                    this.getView().showErrorNotification("输入的统一社会信用代码在客户单据中存在！！");
                    return;
                }
                DynamicObject bd_supplier = BusinessDataServiceHelper.loadSingle("bd_supplier", qFilters);//供应商数据
                if (bd_supplier != null) {
                    String supplierNumber = bd_supplier.getString("number");//供应商编码
                    if (!number.equals(supplierNumber)) {
                        this.getModel().setValue("societycreditcode", "");
                        this.getView().showErrorNotification("输入的统一社会信用代码在供应商单据中存在！！");
                        return;
                    }
                }
            }
        }
    }
}
